<?php
namespace Admin\Controller;
use Think\Controller;
class LizhiController extends BaseController {
    
    public function lzygzy(){
        //获取离职员工资源
        $resourceModel = D('resource');
        $resourceInfo = $resourceModel->liZhiInfo();
        //获取本部门人员
        //经理获取主任
        $userModel = D('user');
        if(session('roleid')==1){
            $where = array('did' =>session('did') ,'roleid'=>2 );
        }elseif(session('roleid')==2){
            $where = array('did' =>session('did') ,'gid'=>session('gid'),'roleid'=>3 );
        }
        $user = $userModel->where($where)->select();
        
        $this->assign(array(
            'zcount'=>$resourceInfo['zcount'],
            'resource' =>$resourceInfo, 
            'resourceInfo'=>$resourceInfo['data'],
            'page'=>$resourceInfo['page'],
            'count'=>$resourceInfo['count'],
            'resPage'=>$resourceInfo['resPage'],
            'user'=>$user,
            ));
    	$this->display();
    }
     //修改资源
    public function update(){
        $data['id'] = I('post.resourceId');
        $data['sid'] = I('post.statusId');
        $data['wechatname'] = i('post.wechatName');
        $data['updatetime'] = time();
        $resourceModel = D('resource');
        $res = $resourceModel->save($data);
        if($res!==FALSE){
            echo json_encode(array('ok' => 1, ));
        }else{
            echo json_encode(array('ok' => 2, ));
        }
    }
    //分配资源
    public function allocate(){
       $userid = I('post.userid');
       $statusId = I('post.statusId');
       $count = I('post.count');
       //查出分配人员信息
       $userModel = D('user');
       $user = $userModel->field('did,gid')->find($userid);
          $did=$user['did'];
          $gid=$user['gid'];
          $onjob=0;
          $updatetime=time();
       //分配数据
       $where = array('onjob' =>1 , 'sid' =>$statusId);
       if($statusId==3){
          $where = array('onjob' =>1 , 'sid' =>array('in',array('6','7','8','9')));
       }
       $resourceModel = D('resource');
       $cou = floor($count/100000);
       $yushu = $count-($cou*100000);
       if($yushu){
          $num=$cou+1;
       }else{
          $num=$cou;
      }
      for ($i=1; $i <= $num; $i++) { 
            if($i==$num && $yushu!=""){
                $count= $yushu;
            }else{
                 $count=100000;
            }
            $resourceInfo = $resourceModel->where($where)->field('id')->limit(0,$count)->select();

            $arr = array();
            foreach ($resourceInfo as $k=>$r) {
               foreach ($r as $k => $v) {
                  $arr[] = $v;
               }    
            }         
            $exp = implode(',',$arr);
            $sql = "UPDATE `cms_resource` SET userid='".$userid."',updatetime='".$updatetime."',onjob='".$onjob."' WHERE id in (".$exp.");";
            $res = $resourceModel->query($sql);
      }
       if($res!==FALSE){
          echo json_encode(array('ok' =>1 , ));
       }else{
          echo json_encode(array('ok' =>2 , ));
       }
        
    }
}